<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }

  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
      return $default;
    }
  }
  
  public static function getShortName($str, $num) {
    if (strlen($str) <= $num) {
      return $str;
    }
    return mb_substr($str, 0, $num, 'utf-8') . '...';
  }

  public static function digits($value, $size) {
    $prefix = '';
    for ($i = 0, $il = $size - strlen($value); $i < $il; $i++) {
      $prefix .= '0';
    }
    return $prefix . $value;
  }

  public static function unique() {
    $chars = array();
    for ($i = 48, $il = 58; $i < $il; $i++) {
      $chars[] = chr($i);
    }
    for ($i = 65, $il = 91; $i < $il; $i++) {
      $chars[] = chr($i);
    }
    for ($i = 97, $il = 123; $i < $il; $i++) {
      $chars[] = chr($i);
    }
    
    $unique = '';
    for ($i = 0, $il = 16; $i < $il; $i++) {
      $unique .= $chars[rand(0, count($chars) - 1)];
    }
    return $unique;
  }

  public static function createMenuInst($arr, $node) {
    if (!isset($node) || $node == NULL) {
      $node = new stdClass();
      $node->id = 0;
      $node->name = 'ROOT';
      $node->order = -99999999;
      $node->c = array();
      MiscUtils::createMenuInst($arr, $node);
      return $node;
    }

    for ($i = 0, $il = count($arr); $i < $il; $i++) {
      $tmp = $arr[$i];
      if ($tmp->parent_id == $node->id) {
        $n = $tmp;
        $n->c = array();
        $node->c[] = $n;
        MiscUtils::createMenuInst($arr, $n);
      }
    }
  }

  public static function rrmdir($dir) {
    if (is_dir($dir)) {
      $objects = scandir($dir);
      foreach ($objects as $object) {
        if ($object != "." && $object != "..") {
          if (filetype($dir . "/". $object) == "dir") {
            MiscUtils::rrmdir($dir . "/". $object);
          } else {
            unlink($dir . "/" . $object);
          }
        }
      }
      reset($objects);
      rmdir($dir);
    }
  }
  
  public static function formatDate($str, $lang, $noTime=true) {
    if ($str == '0000-00-00 00:00:00') {
      return '-';
    }
    
    $tmp1 =  split(" ", $str);
    $tmp2 = split("-", $tmp1[0]);
    $tmp3 = split(":", $tmp1[1]);
    
    $year = $tmp2[0];
    $month = $tmp2[1];
    $day = $tmp2[2];
    $hour = $tmp3[0];
    $minute = $tmp3[1];
    $second = $tmp3[2];
    
    if ($lang == 'zh_CN' || $lang == 'zh_TW') {
      if (!$noTime) {
        return $tmp1[0] . ' ' . $hour . ':' . $minute;
      } else {
        return $tmp1[0];
      }
    } else if ($lang == 'en') {
      if (!$noTime) {
        return $day . ' ' . MiscUtils::getMonth($month) . ' ' . substr($year, 2) . ', ' . $hour . ':' . $minute;
      } else {
        return $day . ' ' . MiscUtils::getMonth($month) . ' ' . substr($year, 2);
      }
    }
    return $lang;
  }
  
  public static function getMonths() {
    return array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  }
  
  public static function getMonth($n) {
    $m = MiscUtils::getMonths();
    return $m[$n - 1];
  }
  
  public static function getDay($n) {
    $d = MiscUtils::getDays();
    return $d[$n];
  }
  
  public static function getDays() {
    return array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
  }
  
  public static function pluginObj($client_id = 0, $order = 0, $attr1 = '', $attr2 = '', $attr3 = '', $attr4 = '', $attr5 = '', $attr6 = '', $attr7 = '', $attr8 = '', $file = '', $path1 = '', $path2 = '', $path3 = '') {
    $obj = new stdClass();
    $obj->track_id = MiscUtils::unique();
    $obj->client_id = $client_id;
    $obj->order = $order;
    $obj->date1 = date("Y-m-d H:i:s");
    $obj->date2 = date("Y-m-d H:i:s");
    $obj->date3 = date("Y-m-d H:i:s");
    $obj->attr1 = $attr1;
    $obj->attr2 = $attr2;
    $obj->attr3 = $attr3;
    $obj->attr4 = $attr4;
    $obj->attr5 = $attr5;
    $obj->attr6 = $attr6;
    $obj->attr7 = $attr7;
    $obj->attr8 = $attr8;
    $obj->file = $file;
    $obj->path1 = $path1;
    $obj->path2 = $path2;
    $obj->path3 = $path3;
    return $obj;
  }
  
  public static function customerId($id) {
    if ($id < 10000 && $id >= 1000) {
      $id = $id;
    } else if ($id < 1000 && $id >= 100) {
      $id = '0' . $id;
    } if ($id < 100 && $id >= 10) {
      $id = '00' . id;
    } if ($id < 10 && $id > 0) {
      $id = '000' . $id;
    }
    return $id;
  }
  
  public static function dateFormat($str) {
    if ($str == '0000-00-00 00:00:00') {
      return '-';
    }
    
    $tmp1 =  split(" ", $str);
    $tmp2 = split("-", $tmp1[0]);
    $tmp3 = split(":", $tmp1[1]);
    
    $year = $tmp2[0];
    $month = $tmp2[1];
    $day = $tmp2[2];
    $hour = $tmp3[0];
    $minute = $tmp3[1];
    $second = $tmp3[2];
    
    return $month . '/' . $day . '/' . $year;
  }
  
  public static function money_format($format, $number) {
    $regex  = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
              '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/'; 
    if (setlocale(LC_MONETARY, 0) == 'C') {
      setlocale(LC_MONETARY, '');
    }
    $locale = localeconv();
    preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
    foreach ($matches as $fmatch) {
      $value = floatval($number);
      $flags = array(
            'fillchar'  => preg_match('/\=(.)/', $fmatch[1], $match) ? 
      $match[1] : ' ',
            'nogroup'   => preg_match('/\^/', $fmatch[1]) > 0, 
            'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? 
      $match[0] : '+',
            'nosimbol'  => preg_match('/\!/', $fmatch[1]) > 0, 
            'isleft'    => preg_match('/\-/', $fmatch[1]) > 0 
      );
      $width      = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
      $left       = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
      $right      = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
      $conversion = $fmatch[5];

      $positive = true;
      if ($value < 0) {
        $positive = false;
        $value  *= -1;
      }
      $letter = $positive ? 'p' : 'n';

      $prefix = $suffix = $cprefix = $csuffix = $signal = '';

      $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
      switch (true) {
        case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
          $prefix = $signal;
          break;
        case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
          $suffix = $signal;
          break;
        case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
          $cprefix = $signal;
          break;
        case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
          $csuffix = $signal;
          break;
        case $flags['usesignal'] == '(':
        case $locale["{$letter}_sign_posn"] == 0:
          $prefix = '(';
          $suffix = ')';
          break;
      }
      if (!$flags['nosimbol']) {
        $currency = $cprefix .
        ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
        $csuffix;
      } else {
        $currency = '';
      }
      $space  = $locale["{$letter}_sep_by_space"] ? ' ' : '';

      $value = number_format($value, $right, $locale['mon_decimal_point'],
      $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
      $value = @explode($locale['mon_decimal_point'], $value);

      $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
      if ($left > 0 && $left > $n) {
        $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
      }
      $value = implode($locale['mon_decimal_point'], $value);
      if ($locale["{$letter}_cs_precedes"]) {
        $value = $prefix . $currency . $space . $value . $suffix;
      } else {
        $value = $prefix . $value . $space . $currency . $suffix;
      }
      if ($width > 0) {
        $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
        STR_PAD_RIGHT : STR_PAD_LEFT);
      }

      $format = str_replace($fmatch[0], $value, $format);
    }
    return $format;
  }
}
?>